package net.zhengxinyun.performance.service.login;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.mapper.login.TokenEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.DatatypeConverter;
import java.util.Date;

/**
 * @Auther: hyp
 * @Date: 2018/10/30 14:33
 */
@Service
public class TokenServiceImpl implements TokenService {

    private TokenEntityMapper tokenEntityMapper;

    @Autowired
    public TokenServiceImpl(TokenEntityMapper tokenEntityMapper) {
        this.tokenEntityMapper = tokenEntityMapper;
    }

    @Override
    public int delete(TokenEntity tokenEntity) {
        return tokenEntityMapper.deleteByPrimaryKey(tokenEntity.getId());
    }

    @Override
    public int update(TokenEntity tokenEntity) {
        return tokenEntityMapper.updateByPrimaryKey(tokenEntity);
    }

    @Override
    public int insert(TokenEntity tokenEntity) {
        return tokenEntityMapper.insert(tokenEntity);
    }

    @Override
    public TokenEntity queryByKyeId(Long id) {
        return tokenEntityMapper.selectByPrimaryKey(id);
    }

    @Override
    public TokenEntity queryByValue(String value) {
        return tokenEntityMapper.queryByValue(value);
    }

    @Override
    public int deleteByUserId(Long userId) {
        return tokenEntityMapper.deleteByUserId(userId);
    }

    @Override
    public TokenEntity queryByUserId(long userId) {
        return tokenEntityMapper.queryByUserId(userId);
    }

    /**
     * token比较
     * @param webToken
     * @return
     */
    @Override
    public boolean compareToken(String webToken)  {
        if ("".equals(webToken) || webToken == null){
            return false;
        }
        TokenEntity localTokenEntity = new TokenEntity();
        try {
            Claims claims = Jwts.parser().setSigningKey(DatatypeConverter.parseBase64Binary("4234dfasdfqdd")).parseClaimsJws(webToken).getBody();
            Long userId = Long.parseLong(claims.getId());
            localTokenEntity = queryByUserId(userId);
        }catch (Exception e){
            return false;
        }
        if (localTokenEntity == null){
            return false;
        }
        if (webToken.equals(localTokenEntity.getValue())){
            Long hour =  DateUtils.pastHour(localTokenEntity.getPostTime());
            if (hour > 2 ){
                return false;
            }else {
                localTokenEntity.setPostTime(new Date());
                tokenEntityMapper.updateForPostTime(localTokenEntity);
                return true;
            }
        }else {
            return false;
        }
    }

    /**
     * 更新token最后请求时间
     * @param tokenEntity
     * @return
     */
    @Override
    public int updateForPostTime(TokenEntity tokenEntity) {
        tokenEntity.setPostTime(new Date());
        return tokenEntityMapper.updateForPostTime(tokenEntity);
    }

}
